package coloring.utils;

import java.util.Set;
import java.util.TreeSet;

import org.jgrapht.Graph;

import coloring.MyDefaultEdge;
import coloring.vertex.Vertex;

/**
 * Some auxiliary methods for graph coloring problem
 * 
 * @author Croaker
 * 
 */
public class Util {

	/**
	 * Returns number of colors
	 * 
	 * @param graph
	 * @return
	 */
	public static int colorNumber(Graph<Vertex, MyDefaultEdge> graph) {
		Set<Integer> colors = new TreeSet<Integer>();
		for (Vertex vertex : graph.vertexSet()) {
			colors.add(vertex.getColor());
		}
		return colors.size();
	}

	/**
	 * Represents string presentation of graph (more human-friendly)
	 * 
	 * @param graph
	 * @return
	 */
	public static String reRepresentGraph(String graph) {
		StringBuilder newGraph = new StringBuilder("(vertices: ");
		newGraph.append(graph.substring(1, graph.indexOf("]")));
		newGraph.append("]\n Edges: ");
		newGraph.append(graph.substring(graph.indexOf("]") + 2));
		return newGraph.toString();
	}

	/**
	 * Checks if this set contains this color
	 * 
	 * @param vertices
	 * @param color
	 * @return
	 */
	public static boolean containsColor(Set<Vertex> vertices, int color) {
		for (Vertex vertex : vertices) {
			if (vertex.getColor() == color) {
				return true;
			}
		}
		return false;
	}
}
